package com.zmn.oms.third.callcenter.service.impl;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.third.base.dto.api.ApiInvokResult;
import com.zmn.oms.third.callcenter.CallCenterApiInvoker;
import com.zmn.oms.third.callcenter.CallCenterConstants;
import com.zmn.oms.third.callcenter.dto.PushOrderCluesDTO;
import com.zmn.oms.third.callcenter.service.CallCenterService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * 类描述：呼叫中心服务实现类
 *
 * @author lhl
 * @date 2020/02/13 18:18
 */
@Service("callCenterService")
public class CallCenterServiceImpl implements CallCenterService {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private ChannelListRemoteService channelListRemoteService;

    @NacosValue(value="${call.order.add.useNewInterface:false}", autoRefreshed = true)
    private Boolean useNewInterface = false;

    @Override
    public ResultDTO pushOrderClues2CallCenter(PushOrderCluesDTO pushOrderCluesDTO) {
        if (StringUtils.isBlank(pushOrderCluesDTO.getChannelName())) {
            // 校验渠道ID
            ResponseDTO<ChannelDRO> channelResponseDTO = channelListRemoteService.getByChannelId(pushOrderCluesDTO.getChannelId());
            ChannelDRO channelDRO = channelResponseDTO.getData();
            if (channelDRO == null) {
                return ResultDTO.fail(String.format("未找到对应的渠道，渠道ID: [%s]", pushOrderCluesDTO.getChannelId()));
            }

            pushOrderCluesDTO.setChannelName(channelDRO.getName());
        }

        ApiInvokResult apiInvokResult = null;
        if (useNewInterface) {
            apiInvokResult = CallCenterApiInvoker.apiInvoke(pushOrderCluesDTO);
        }
        else {
            apiInvokResult = CallCenterApiInvoker.apiInvokeOld(CallCenterConstants.PATH_ADD_ORDER, pushOrderCluesDTO);
        }

        if (apiInvokResult.isSuccess()) {
            return ResultDTO.success();
        }
        else {
            return ResultDTO.fail(apiInvokResult.getMsg());
        }
    }

}
